<!-- 页面外套 -->
<div class="page-wrapper" id="personnel_interview">

    <!-- 数据网格 -->
	<div class="datagrid datagrid-striped" id="personnel_interview_datagrid">

        <!-- 工具条组 -->
        <div class="tool-group">
            <div class="tool-search">
                <div class="im-label">
                    <div class="input-group">
                        <select class="form-control" id="personnel_interview_tool_keys">
                            <option value="1">姓名</option>
                            <option value="2">电话</option>
                        </select>
                        <span class="input-group-addon fix-border fix-padding"></span>
                        <input type="text" class="form-control" id="personnel_interview_tool_search">
                    </div>
                </div>
                <div id="personnel_interview_tool_select_group"><!--JS推进--></div>   
                <label>
                    <span>状态：</span>
                    <select class="form-control" id="personnel_interview_tool_select2"><!--JS推进--></select>
                </label>
                <div class="im-label" id="personnel_interview_tool_date"><!--JS推进--></div>
                <label><button type="button" class="btn btn-primary" id="personnel_interview_search_btn"><i class="icon icon-search"></i> 搜索</button></label>
                <div class="clearfix"></div>
            </div>
            <div class="tool-deal">
                <label>
                    <div class="btn-group" data-toggle="buttons">
                        <label id="personnel_interview_che1_btn" class="btn btn-primary active"><input type="radio">邀约面试</label>
                        <label id="personnel_interview_che2_btn" class="btn btn-primary"><input type="radio">人才库</label>
                    </div>
                </label>
                <label><button type="button" class="btn btn-primary" id="personnel_interview_add_btn">   <i class="icon icon-plus"></i> 添加</button></label>
                <div class="clearfix"></div>
            </div>
        </div>
        <!-- tool-group -->

        <div class="datagrid-container"></div>
        <ul class="pager btn-mini" data-elements="prev,pages,next"></ul>
    </div>
    <!-- datagrid -->

    <!-- 添加盒子 -->
    <div class="modal modal-for-page fade" aria-hidden="false" id="personnel_interview_add_box">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">关闭</span></button>
                    <h4 class="modal-title">添加</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_add_user">姓名</label>
                            <div class="col-sm-8"><input type="text" class="form-control" id="personnel_interview_add_user"></div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required">性别</label>
                            <div class="col-sm-8">
                                <div class="btn-group" data-toggle="buttons" id="personnel_interview_add_sexy">
                                    <label class="btn active"><input type="radio" name="personnel_interview_add_sexy" value="1">先生</label>
                                    <label class="btn">       <input type="radio" name="personnel_interview_add_sexy" value="2">女士</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_add_tels">电话</label>
                            <div class="col-sm-8"><input type="text" class="form-control" id="personnel_interview_add_tels"></div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_add_date">面试时间</label>
                            <div class="col-sm-8"><input class="form-control" id="personnel_interview_add_date" readonly></div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_add_post">应聘职位</label>
                            <div class="col-sm-8"><select class="form-control" id="personnel_interview_add_post"><!-- JS推进 --></select></div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_add_copy">抄送</label>
                            <div class="col-sm-8"><select class="form-control" id="personnel_interview_add_copy"><!-- JS推进 --></select></div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="personnel_interview_add_submit">保存</button>
                </div>
            </div>
        </div>
    </div>
    <!-- personnel_interview_add_box -->

    <!-- 修改盒子 -->
    <div class="modal modal-for-page fade" aria-hidden="false" id="personnel_interview_edit_box">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">关闭</span></button>
                    <h4 class="modal-title">修改</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_edit_user">姓名</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="personnel_interview_edit_user">
                            </div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required">性别</label>
                            <div class="col-sm-8">
                                <div class="btn-group" data-toggle="buttons" id="personnel_interview_edit_sexy">
                                    <label class="btn"><input type="radio" name="personnel_interview_edit_sexy" value="1">先生</label>
                                    <label class="btn"><input type="radio" name="personnel_interview_edit_sexy" value="2">女士</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_edit_tels">电话</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="personnel_interview_edit_tels">
                            </div>
                        </div>
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_edit_post">应聘职位</label>
                            <div class="col-sm-8"><select class="form-control" id="personnel_interview_edit_post"><!-- JS推进 --></select></div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="personnel_interview_edit_submit">保存</button>
                </div>
            </div>
        </div>
    </div>
    <!-- personnel_interview_edit_box -->

    <!-- 状态盒子 -->
    <div class="modal modal-for-page fade" aria-hidden="false" id="personnel_interview_state_box">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">关闭</span></button>
                    <h4 class="modal-title"><!-- JS推进 --></h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group require">
                            <label class="col-sm-2 required" for="personnel_interview_state_mess">审批意见</label>
                            <div class="col-sm-8"><input type="text" class="form-control" id="personnel_interview_state_mess"></div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="personnel_interview_state_submit">保存</button>
                </div>
            </div>
        </div>
    </div>
    <!-- personnel_interview_state_box -->

</div>
<!-- page-wrapper -->
<script>
$(function(){
    
    //变量声明-----------------------------------------------------------------------------------------------
    var 
    personnel_interview                  = $("#personnel_interview"),                   //页面ID
    personnel_interview_datagrid         = $("#personnel_interview_datagrid"),          //数据表格
    //搜索
    personnel_interview_tool_keys        = $("#personnel_interview_tool_keys"),         //搜索类别
    personnel_interview_tool_search      = $("#personnel_interview_tool_search"),       //搜索字段
    personnel_interview_tool_select_group= $("#personnel_interview_tool_select_group"), //下拉框组
    personnel_interview_tool_select2     = $("#personnel_interview_tool_select2"),      //下拉选项2 
    personnel_interview_tool_date        = $("#personnel_interview_tool_date"),         //起止时间
    //按钮
    personnel_interview_search_btn       = $("#personnel_interview_search_btn"),        //搜索按钮
    personnel_interview_add_btn          = $("#personnel_interview_add_btn"),           //添加按钮
    personnel_interview_che1_btn         = $("#personnel_interview_che1_btn"),          //邀约面试
    personnel_interview_che2_btn         = $("#personnel_interview_che2_btn"),          //人才库
    //添加
    personnel_interview_add_btn          = $("#personnel_interview_add_btn"),           //添加按钮
    personnel_interview_add_box          = $("#personnel_interview_add_box"),           //添加按钮
    personnel_interview_add_submit       = $("#personnel_interview_add_submit"),        //添加提交
    personnel_interview_add_user         = $("#personnel_interview_add_user"),          //姓名字段
    personnel_interview_add_sexy         = $("#personnel_interview_add_sexy"),          //性别字段
    personnel_interview_add_tels         = $("#personnel_interview_add_tels"),          //电话字段
    personnel_interview_add_date         = $("#personnel_interview_add_date"),          //时间字段
    personnel_interview_add_post         = $("#personnel_interview_add_post"),          //职位字段
    personnel_interview_add_copy         = $("#personnel_interview_add_copy"),          //抄送字段
    //修改
    personnel_interview_edit_box         = $("#personnel_interview_edit_box"),          //修改按钮
    personnel_interview_edit_submit      = $("#personnel_interview_edit_submit"),       //修改提交
    personnel_interview_edit_user        = $("#personnel_interview_edit_user"),         //姓名字段
    personnel_interview_edit_sexy        = $("#personnel_interview_edit_sexy"),         //性别字段
    personnel_interview_edit_tels        = $("#personnel_interview_edit_tels"),         //电话字段
    personnel_interview_edit_post        = $("#personnel_interview_edit_post"),         //职位字段
    //状态
    personnel_interview_state_box        = $("#personnel_interview_state_box"),         //状态盒子
    personnel_interview_state_submit     = $("#personnel_interview_state_submit"),      //状态提交
    personnel_interview_state_mess       = $("#personnel_interview_state_mess");        //状态字段 

    //接口对象-----------------------------------------------------------------------------------------------
    if( window.STATE == "local" ){

        //本地接口
        var personnel_interview_api = {
            
            select_comp     : LOCAL + "Company/getCompanyByPositionid.json",    //分公司下拉
            select_state1   : LOCAL + "Interview/GetState.json",                //状态下拉(邀约面试)
            select_state2   : LOCAL + "Interview/GetState.json",                //状态下拉(人才库)
            select_copy     : LOCAL + "Interview/GetCSR.json",                  //抄送人下拉
            modal_add       : LOCAL + "Interview/AddInterview.json",            //添加提交
            modal_edit_read : LOCAL + "Interview/UpdateIvDisplay.json",         //修改读取
            modal_edit      : LOCAL + "Interview/UpdateInterview.json",         //修改提交
            modal_state     : LOCAL + "Interview/UpdateInterviewState.json",    //状态提交
            datagrid        : LOCAL + "Interview/GetInterview.json",            //数据表格
        }

    } else if ( window.STATE == "route" ){

        //远程接口
        var personnel_interview_api = {
            select_comp     : ROUTE + "Company/getCompanyByPositionid",         //分公司下拉
            select_state1   : ROUTE + "Interview/GetState",                     //状态下拉(邀约面试)
            select_state2   : ROUTE + "Interview/GetState",                     //状态下拉(人才库)
            select_copy     : ROUTE + "Interview/GetCSR",                       //抄送人下拉
            modal_add       : ROUTE + "Interview/AddInterview",                 //添加提交
            modal_edit_read : ROUTE + "Interview/UpdateIvDisplay",              //修改读取
            modal_edit      : ROUTE + "Interview/UpdateInterview",              //修改提交
            modal_state     : ROUTE + "Interview/UpdateInterviewState",         //状态提交
            datagrid        : ROUTE + "Interview/GetInterview",                 //数据表格
        }

    }

    //数据表格-----------------------------------------------------------------------------------------------
    personnel_interview_datagrid.datagrid({
        height      : zui_datagrid_height(personnel_interview_datagrid),
        sortable    : false,
        cache       : false,
        showRowIndex: true,
        configs     : {
            R0:{className:"text-center"},
            C1:{className:"cell-hidden"}
        },
        states      : {
            fixedLeftUntil  : 2,
            pager           : {
                page        : 1,
                recPerPage  : window.REC_PER_PAGE,
            }
        },
        dataSource : {
            cols   : [
                {name:"id",          label:"ID"  ,    width:49 },
                {name:"name",        label:"姓名",    width:80 },
                {name:"tel" ,        label:"电话",    width:100},
                {name:"positionname",label:"应聘职位",width:80 },
                {name:"sex",         label:"性别",    width:50 },
                {name:"meettime",    label:"面试时间",width:120},
                {name:"statename",   label:"状态",    width:85 },
                {name:"copyname",    label:"抄送",    width:80 },
                {name:"examine" ,    label:"审批意见",width:240,
                    html:true,
                    valueOperator : {
                        getter : function(dataValue,cell,dataGrid){
                            return `<a title="点击查看" data-toggle="modal" data-title="审批意见" data-custom="${dataValue}">${dataValue}</a>`;
                        }
                    }
                },
                {name:"state",label:"操作",width:140,
                    html:true,
                    valueOperator : {
                        getter : function(dataValue,cell){
                            var temp = "";
                            //待面试
                            if( dataValue==1 ){
                                temp = `
                                    <a class="personnel_interview_edit_btn"  rowIndex="${cell.rowIndex}">修改</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="2">通过</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="4">淘汰</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="5">储备</a>
                                `;
                            }
                            //初试通过
                            if( dataValue==2 ){
                                temp = `
                                    <a class="personnel_interview_edit_btn"  rowIndex="${cell.rowIndex}">修改</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="3">通过</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="4">淘汰</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="5">储备</a>
                                `;
                            }
                            //复试通过
                            if( dataValue==3 ){
                                temp = `
                                    <a class="personnel_interview_edit_btn"  rowIndex="${cell.rowIndex}">修改</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="5">储备</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="7">录用</a>
                                `;
                            }
                            //淘汰+储备
                            if( dataValue==4 || dataValue==5 ){
                                temp = `
                                    <a class="personnel_interview_edit_btn"  rowIndex="${cell.rowIndex}">修改</a>
                                    <a class="personnel_interview_state_btn" rowIndex="${cell.rowIndex}" state="7">录用</a>
                                `;
                            }
                            return temp;
                        }
                    }
                },
            ],
            remote : function(){
                return {
                    url     : personnel_interview_api.datagrid+"?bs=1",
                    type    : "GET",
                    dataType: "json"
                }
            }
        }
    });

    //变量声明-----------------------------------------------------------------------------------------------
    var
    personnel_interview_datagrid_obj= personnel_interview_datagrid.data("zui.datagrid"),//表格对象
    personnel_interview_edit_btn    = ".personnel_interview_edit_btn",                  //修改
    personnel_interview_state_btn   = ".personnel_interview_state_btn";                 //状态

    //下拉组件-----------------------------------------------------------------------------------------------
    common_select_linkage("#personnel_interview_tool_select_group","2");                //搜索

    //时间组件-----------------------------------------------------------------------------------------------
    common_date_duration( personnel_interview_tool_date );
    personnel_interview_tool_date.find(".start").datetimepicker(option_date);
    personnel_interview_tool_date.find(".end").datetimepicker(option_date);
    personnel_interview_add_date.datetimepicker(option_hour);

    //下拉选项-----------------------------------------------------------------------------------------------
    //职位(添加+修改)
    $.ajax({
        url     : API.common_post,
        type    : "get",
        dataType: "json",
        data    : { },
        success : function(data){
            var data = data.data;
            var option = "";
            for(var i=0;i<data.length;i++){
                option += "<option value='"+ data[i].id +"'>"+data[i].name+"</option>"
            }
            personnel_interview_add_post.html(option);
            personnel_interview_edit_post.html(option);
        }
    });    

    //抄送人(添加+修改)
    $.ajax({
        url     : personnel_interview_api.select_copy,
        type    : "get",
        dataType: "json",
        data    : {
            positionid : COMMON.position_id
        },
        success : function(data){
            var data = data.data;
            var option = "";
            for(var i=0;i<data.length;i++){
                option += "<option value='"+ data[i].id +"'>"+data[i].name+"</option>";
            }
            personnel_interview_add_copy.html(option);              //工具条职位
        }
    });

    //邀约面试-状态
    $.ajax({
        url     : personnel_interview_api.select_state1,
        type    : "get",
        dataType: "json",
        data    : {
            type : "0"
        },
        success : function(data){

            var data = data.data;
            var option = "<option value='0'>全部</option>";
            for(var i=0;i<data.length;i++){
                option += "<option value='"+ data[i].id +"'>"+data[i].statename+"</option>"
            }
            personnel_interview_tool_select2.html(option);

        }
    });
    //邀约面试-搜索
    personnel_interview_search_btn.attr("bs",1);

    //右侧切换-----------------------------------------------------------------------------------------------
    //邀约面试
    personnel_interview_che1_btn.click(function(){

        //状态(邀约面试)
        $.ajax({
            url     : personnel_interview_api.select_state1,
            type    : "get",
            dataType: "json",
            data    : {
                type : "0"
            },
            success : function(data){

                //下拉
                var data = data.data;
                var option = "<option value='0'>全部</option>";
                for(var i=0;i<data.length;i++){
                    option += "<option value='"+ data[i].id +"'>"+data[i].statename+"</option>"
                }
                personnel_interview_tool_select2.html(option);

                //搜索按钮
                personnel_interview_search_btn.attr("bs",1);

                //添加按钮
                personnel_interview_add_btn.show();

                //表格
                zui_datagrid_render(personnel_interview_datagrid_obj,personnel_interview_api.datagrid+"?bs=1");

            }
        });

    });
    //人才库
    personnel_interview_che2_btn.click(function(){

        //状态(人才库)
        $.ajax({
            url     : personnel_interview_api.select_state2,
            type    : "get",
            dataType: "json",
            data    : {
                type : "1"
            },
            success : function(data){

                //下拉
                var data = data.data;
                var option = "<option value='0'>全部</option>";
                for(var i=0;i<data.length;i++){
                    option += "<option value='"+ data[i].id +"'>"+data[i].statename+"</option>"
                }
                personnel_interview_tool_select2.html(option);

                //搜索按钮
                personnel_interview_search_btn.attr("bs",2);

                //添加按钮
                personnel_interview_add_btn.hide();

                //表格
                zui_datagrid_render(personnel_interview_datagrid_obj,personnel_interview_api.datagrid+"?bs=2");

            }
        });

    });

    //搜索按钮-----------------------------------------------------------------------------------------------
    personnel_interview_search_btn.click(function(){

        //字段整理
        var keyword
        = "?sid="       + personnel_interview_tool_keys.val()
        + "&search="    + personnel_interview_tool_search.val()
        + "&starttime=" + personnel_interview_tool_date.find(".start").val()
        + "&endtime="   + personnel_interview_tool_date.find(".end").val()
        + "&positionid="+ personnel_interview_tool_select_group.find(".common_select_post").val()
        + "&isstate="   + personnel_interview_tool_select2.val()
        + "&bs="        + personnel_interview_search_btn.attr("bs");

        //GET提交
        zui_datagrid_render(personnel_interview_datagrid_obj,personnel_interview_api.datagrid+keyword);

    });

    //添加按钮------------------------------------------------------------------------
    personnel_interview_add_btn.click(function(){
        common_form_reset();
        personnel_interview_add_box.modal("show");
    });
    //添加提交
    personnel_interview_add_submit.click(function(){

        //远程提交
        $.ajax({
            url     : personnel_interview_api.modal_add,
            type    : "post",
            dataType: "json",
            data    : {
                userid    : COMMON.user_id,
                name      : personnel_interview_add_user.val(),
                sex       : personnel_interview_add_sexy.find(".active input").val(),
                tel       : parseInt(personnel_interview_add_tels.val()),
                datetime  : personnel_interview_add_date.val(),
                positionid: personnel_interview_add_post.val(),
                copyid    : personnel_interview_add_copy.val()
            },
            success : function(data){
                if( data.status>0){
                    um_tip(data.message);
                    zui_datagrid_reset(personnel_interview_datagrid_obj,personnel_interview_api.datagrid);
                } else {
                    um_tip(data.message,"1500","text-danger");
                }
            }
        });

    });

    //修改按钮-----------------------------------------------------------------------------------------------
    $(document).on("click",personnel_interview_edit_btn,function(){ 

        //数据ID
        var data_id = zui_datagrid_get_id($(this).attr("rowIndex"));

        //远程提交
        $.ajax({
            url     : personnel_interview_api.modal_edit_read,
            type    : "post",
            dataType: "json",
            data    : { id:data_id },
            success : function(data){
                if( data.status>0 ){

                    //数据赋值
                    var data = data.data[0];
                    personnel_interview_edit_user.val( data.name );
                    personnel_interview_edit_tels.val( data.tel );
                    var sex_num =1; ( data.sex=="男" ) ? sex_num=1 : sex_num=2;
                    $("#personnel_interview_edit_sexy label").removeClass("active");
                    $("#personnel_interview_edit_sexy label:nth-child("+ sex_num +")").addClass("active");
                    personnel_interview_edit_post.val( data.positionid );

                    //其他操作
                    personnel_interview_edit_submit.attr("rowIndex",data_id);                   //数据ID传值
                    personnel_interview_edit_box.modal("show");                                 //盒子显示

                } else {
                    um_tip(data.message,"1500","text-danger");
                }
            }
        });

    });
    //修改提交
    personnel_interview_edit_submit.click(function(){

        //变量声明
        var data_id = $(this).attr("rowIndex");
        var personnel_interview_edit_sexy = $("#personnel_interview_edit_sexy .active input");

        //远程提交
        $.ajax({
            url     : personnel_interview_api.modal_edit,
            type    : "post",
            dataType: "json",
            data    : { 
                id         : data_id,
                name       : personnel_interview_edit_user.val(),
                sex        : personnel_interview_edit_sexy.val(),
                tel        : personnel_interview_edit_tels.val(),
                positionid : personnel_interview_edit_post.val(),
            },
            success : function(data){
                if( data.status>0 ){
                    um_tip(data.message);
                    zui_datagrid_reset(personnel_interview_datagrid_obj,personnel_interview_api.datagrid);
                } else {
                    um_tip(data.message,"1500","text-danger");
                }
            }
        });

    });

    //状态按钮-----------------------------------------------------------------------------------------------
    $(document).on("click",personnel_interview_state_btn,function(){ 

        //变量声明
        var data_id = zui_datagrid_get_id($(this).attr("rowIndex"));                       //数据ID
        var text    = $(this).text();                                               //操作名称
        var state   = $(this).attr("state");                                        //操作代号

        //其他操作
        personnel_interview_state_submit.attr("rowIndex",data_id);                  //数据ID赋值
        personnel_interview_state_submit.attr("state",state);                       //操作代号赋值
        personnel_interview_state_box.find(".modal-title").text( text );            //操作名称赋值
        personnel_interview_state_box.modal("show");                                //盒子显示

    });
    //状态提交
    personnel_interview_state_submit.click(function(){

        //数据ID
        var data_id = $(this).attr("rowIndex");                                     //数据ID
        var state   = $(this).attr("state");                                        //操作代号
        
        //远程提交
        $.ajax({
            url     : personnel_interview_api.modal_state,
            type    : "post",
            dataType: "json",
            data    : { 
                id      : data_id,
                isstate : state,
                message : personnel_interview_state_mess.val()
            },
            success : function(data){
                if( data.status>0 ){
                    um_tip(data.message);
                    zui_datagrid_reset(personnel_interview_datagrid_obj,personnel_interview_api.datagrid);
                } else {
                    um_tip(data.message,"1500","text-danger");
                }
            }
        });

    });

});//预加载结尾
</script>